<?php
/**
 * 网络请求扩展类
 * ver 1.0
 * author Myxf
 */
class Http {
    public $ver = "1.0";
    /**
     * post方法
     * @access public
     * @param string $url 请求地址
     * @param string $data 提交数据
     * @param array $header 附加协议头
     * @param int $head 是否显示返回协议头
     */
    function vpost($url,$data,$header = array(),$head = 0){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, $head);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $tmpInfo = curl_exec($curl);
        if (curl_errno($curl)) {
           return 'Error';
        }
        curl_close($curl);
        return $tmpInfo;
    }

    /**
     * get方法
     * @access public
     * @param string $url 请求地址
     * @param array $header 附加协议头
     * @param int $head 是否显示返回协议头
     */
    function vget($url,$header = array(),$head = 0){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, $head);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $tmpInfo = curl_exec($curl);
        if (curl_errno($curl)) {
           return 'Error';
        }
        curl_close($curl);
        return $tmpInfo;
    }
    
    /**
     * 采集远程数据到本地
     * @access public
     * @param string $url 请求地址
     * @param string $path 保存路径
     */
    function getDataSave($url,$name="",$path="/upload/") {
        $data = $this->vget($url);
        if (empty($name)){
           $name = basename($url);
        }
        if(!is_dir($path)){
            //如果目录不存在，创建目录
            $this->set_dir($path);
        }
        file_put_contents($path.$name,$data);
        $url = $path.$name;
        return $url;
    }

    /**
     * 创建目录
     * @access public
     * @param string $dir 要创建目录的路径
     * @return boolean 失败时返回错误消息和FALSE
     */
    function set_dir($dir = null){
        //检查路径是否存在
        if(is_dir($dir)){
            return false;
        }else{
            $dir = explode('/', $dir);
            foreach($dir as $v){
                if($v){
                    $d .= $v . '/';
                    if(!is_dir($d)){
                        $state = mkdir($d, 0777);
                        if(!$state){
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    }
}
